package com.example.controller;

import com.example.entity.User;
import com.example.service.UserService;
import com.example.util.AjaxResult;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/auth/login")
    private AjaxResult login(@RequestParam String username, @RequestParam String password) {
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("password", password);

        User user = userService.login(map);
        if (ObjectUtils.isNotEmpty(user)){
            return AjaxResult.success("登录成功", user);
        }else{
            return AjaxResult.error("用户名或密码错误");
        }
    }

}
